//
//  SDDefineActionSheetView.m
//  SDAlertView
//
//  Created by sander on 2017/10/24.
//

#import "SDDefineActionSheetView.h"

#import "SDActionSheetTableViewCell.h"

#import "SDAlertActionModel.h"

@interface SDDefineActionSheetView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, weak) UILabel * theTitleLabel;

@property (nonatomic, weak) UITableView * tableView;

@end

@implementation SDDefineActionSheetView

- (instancetype)initWithTitle:(NSString * )title list:(NSArray * )list
{
    self = [super init];
    if (self) {
        _actionSheetTitle = title;
        
        _list = list;
        self.frame = CGRectMake(0, 0, screen_width , 0);
        [self sd_configView];
    }
    return self;
}


- (void)sd_configView
{
    if (self.actionSheetTitle) {
        self.theTitleLabel.text = self.actionSheetTitle;
    }
    
}

- (void)setList:(NSArray *)list
{
    _list = list;
 
    
    CGFloat content_height = 0.f;
    
    if (self.actionSheetTitle) {
        content_height = self.table_height;
    }
    
    if (list.count > 0) {
        
        content_height += self.table_height * list.count;
        
    }
    
    self.frame = CGRectMake(0, 0, screen_width, content_height);
    
    if (!self.actionSheetTitle) {
        self.tableView.frame = self.bounds;
        
    }
   [[self tableView] reloadData];
    
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDActionSheetTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[SDActionSheetTableViewCell CellIdentifier]];
    SDAlertActionModel * model = self.list[indexPath.row];
    cell.cell_string = model.alert_string;
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.table_height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    SDAlertActionModel * model = self.list[row];
    [model dismissViewController];
    
}


- (CGFloat)table_height
{
    if (_table_height == 0) {
        _table_height = 38.0f;
    }
    return _table_height;
}
- (UILabel *)theTitleLabel
{
    if (!_theTitleLabel) {
        UILabel * theView = [[UILabel alloc] init];
        [self addSubview:theView];
        theView.font = [UIFont systemFontOfSize:15];
        theView.textColor = [UIColor colorWithRed:117.f/255.f green:117.0f/255.f blue:117.f/255.f alpha:1];
        theView.textAlignment = NSTextAlignmentCenter;
        theView.frame = CGRectMake(0, 0, self.bounds.size.width, 38);
        _theTitleLabel = theView;
    }
    return _theTitleLabel;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView * theView = [[UITableView alloc] init];
        [self addSubview:theView];
        theView.frame = CGRectMake(0, 38, self.bounds.size.width, self.bounds.size.height - 38);
        theView.dataSource = self;
        
        theView.delegate = self;
        
        theView.scrollEnabled = false;
        
        theView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [theView registerClass:[SDActionSheetTableViewCell class] forCellReuseIdentifier:[SDActionSheetTableViewCell CellIdentifier]];
        
        _tableView = theView;
    }
    return _tableView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
